home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_3 / phoonsrc / xphoon.c < prev   
C/C++ Source or Header  |  1994-02-23  |  12KB  |  336 lines

  1. /*
  2. ** Copyright (C) 1988 by Jef Poskanzer and Craig Leres.
  3. **
  4. ** Permission to use, copy, modify, and distribute this software and its
  5. ** documentation for any purpose and without fee is hereby granted, provided
  6. ** that the above copyright notice appear in all copies and that both that
  7. ** copyright notice and this permission notice appear in supporting
  8. ** documentation.  This software is provided "as is" without express or
  9. ** implied warranty.
  10.  
  11.  *  Last modified: Thu Feb 24 12:27:07 1994 too
  12.  *
  13.  */
  14.  
  15. #include <math.h>
  16. #include "amiga.h"
  17. #include "original/tws.h"
  18.  
  19. #ifndef NULL
  20. #define NULL 0
  21. #endif
  22.  
  23. char *argv0;
  24. int shadeflag = 0;
  25. int demoflag = 0;
  26. int reverseflag = 0;
  27. int doubleflag = 0;
  28.  
  29. void    checkbitmapsize(int *, int *, int, int, int *, int *, int *, int *);
  30. void    getbitmap(int, int, int, int, char **, int *, int *, int *, int *);
  31.  
  32. static void hackbits(struct tws * t, int w,
  33.              char *bits, int cx, int cy, int rx, int ry);
  34.  
  35. #define isdigit(a) (a >= '0' && a <= '9')
  36.  
  37. const char usage[] = "Usage: phoon " TEMPLATE "\n";
  38. LONG args[CI_SIZE] = { 0 };
  39.  
  40. int start()
  41. {
  42.     int w, h, fm_w, fm_h, cx = 0, cy = 0, rx = 0, ry = 0;
  43.     char *bits, *origbits;
  44.     char *filename = NULL;
  45.     int i, value;
  46.  
  47.     if (am_OpenLibraries() == FALSE)
  48.       return am_CloseLibraries(20);
  49.  
  50.     /*
  51.      * am_OpenLibraries did ReadArgs()
  52.      */
  53.     if (args[CI_DOUBLE])
  54.       doubleflag = 1;
  55.  
  56.     filename = (char *)args[CI_FILE];  /* NULL if does not exist */
  57.  
  58.     if (args[CI_POSITION]) {
  59.       char * ptr = (char *)args[CI_POSITION];
  60.       for (i = 0; i < 4; i++) {
  61.         value = (int)ptr[2 * i];
  62.           switch (value) {
  63.           case '\0':    i = 5; continue;
  64.           case '-':     value = -1;     break;
  65.           case '+':     value =  1;     break;
  66.           default:    goto usage;
  67.           }
  68.         if (!isdigit(ptr[2 * i + 1])) {
  69.         usage:
  70.           Printf(usage);
  71.           return am_CloseLibraries(20);
  72.         }
  73.         value = value * (ptr[2 * i + 1] - '0');
  74.         switch (i) {
  75.         case 0:    cx = value;    break;
  76.         case 1:    cy = value;    break;
  77.         case 2:    rx = value;    break;
  78.         case 3:    ry = value;    break;
  79.         }
  80.       }
  81.     }
  82.     if (args[CI_REVERSE])
  83.       reverseflag = 1;
  84.  
  85.     if (args[CI_SHADE])
  86.       shadeflag = 1;
  87.  
  88.     Printf("Phase of the moon, Amiga version 2.1 (24.2.1994)\n");
  89.  
  90.     if (args[CI_GMTOFFSET]) {
  91.       if (gmtoffset != 32767)
  92.         Printf("*** Commmand line GMTOFFSET overruns Locale definition\n");
  93.       gmtoffset = -*(LONG *)args[CI_GMTOFFSET] * 60;
  94.     }
  95.     else if (gmtoffset == 32767) {
  96.       Printf("*** No GMTOFFSET defined... Used timezone is GMT\n");
  97.       gmtoffset = 0;
  98.     }
  99.  
  100.     if (am_GetScreenSize(&w, &h) == FALSE)
  101.       return am_CloseLibraries(20);
  102.  
  103.     if ((bits = am_LoadFullmoon(&fm_w, &fm_h, filename)) == NULL)
  104.       return am_CloseLibraries(20);
  105.  
  106.     origbits = bits;
  107.     checkbitmapsize(&w, &h, fm_w, fm_h, &cx, &cy, &rx, &ry);
  108.  
  109.     getbitmap(w, h, fm_w, fm_h, &bits, &cx, &cy, &rx, &ry);
  110.     hackbits(dtwstime(), w, bits, cx, cy, rx, ry);
  111.  
  112.     am_MakeIFFile(w, h, bits, reverseflag);
  113.     FreeVec(bits);
  114.     return am_CloseLibraries(0);
  115. }
  116.  
  117. short leftmask[16] = {
  118.     0x8000, 0xc000, 0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00,
  119.     0xff80, 0xffc0, 0xffe0, 0xfff0, 0xfff8, 0xfffc, 0xfffe, 0xffff,
  120. };
  121. short rightmask[16] = {
  122.     0x7fff, 0x3fff, 0x1fff, 0x0fff, 0x07ff, 0x03ff, 0x01ff, 0x00ff,
  123.     0x007f, 0x003f, 0x000f, 0x001f, 0x0007, 0x0003, 0x0001, 0x0000,
  124. };
  125.  
  126. static short  shades_0_bits[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  127.                   0x0000,0x0000};
  128. static short  shades_1_bits[] = {0x0101,0x0000,0x0000,0x0000,0x0000,0x0000,
  129.                   0x0000,0x0000};
  130. static short  shades_2_bits[] = {0x0101,0x0000,0x0404,0x0000,0x0000,0x0000,
  131.                   0x0000,0x0000};
  132. static short  shades_3_bits[] = {0x0101,0x0000,0x0404,0x0000,0x8080,0x0000,
  133.                   0x0000,0x0000};
  134. static short  shades_4_bits[] = {0x0101,0x0000,0x0404,0x0000,0x8080,0x0000,
  135.                   0x0000,0x1010};
  136. static short  shades_5_bits[] = {0x0101,0x4040,0x0404,0x0000,0x8080,0x0000,
  137.                   0x0000,0x1010};
  138. static short  shades_6_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8080,0x0000,
  139.                   0x0000,0x1010};
  140. static short  shades_7_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8080,0x0000,
  141.                   0x0101,0x1010};
  142. static short  shades_8_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8080,0x2020,
  143.                   0x0101,0x1010};
  144. static short  shades_9_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8282,0x2020,
  145.                   0x0101,0x1010};
  146. static short shades_10_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8282,0x2020,
  147.                   0x0505,0x1010};
  148. static short shades_11_bits[] = {0x0101,0x4040,0x0404,0x2020,0x8282,0x2020,
  149.                   0x0505,0x5050};
  150. static short shades_12_bits[] = {0x0101,0x4040,0x0505,0x2020,0x8282,0x2020,
  151.                   0x0505,0x5050};
  152. static short shades_13_bits[] = {0x0101,0x5050,0x0505,0x2020,0x8282,0x2020,
  153.                   0x0505,0x5050};
  154. static short shades_14_bits[] = {0x0101,0x5050,0x0505,0x2020,0x7575,0x2020,
  155.                   0x0505,0x5050};
  156. static short shades_15_bits[] = {0x0505,0x5050,0x0505,0x2020,0x7575,0x2020,
  157.                   0x0505,0x5050};
  158.  
  159. short *shades_s[16] = {
  160.     shades_0_bits,  shades_1_bits,  shades_2_bits,  shades_3_bits,
  161.     shades_4_bits,  shades_5_bits,  shades_6_bits,  shades_7_bits,
  162.     shades_8_bits,  shades_9_bits,  shades_10_bits, shades_11_bits,
  163.     shades_12_bits, shades_13_bits, shades_14_bits, shades_15_bits };
  164.  
  165.  
  166. static short  shaded_0_bits[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  167.                   0x0000,0x0000};
  168. static short  shaded_1_bits[] = {0x0003,0x0000,0x0000,0x0000,0x0000,0x0000,
  169.                   0x0000,0x0000};
  170. static short  shaded_2_bits[] = {0x0003,0x0000,0x0030,0x0000,0x0000,0x0000,
  171.                   0x0000,0x0000};
  172. static short  shaded_3_bits[] = {0x0003,0x0000,0x0030,0x0000,0xc000,0x0000,
  173.                   0x0000,0x0000};
  174. static short  shaded_4_bits[] = {0x0003,0x0000,0x0030,0x0000,0xc000,0x0000,
  175.                   0x0000,0x0300};
  176. static short  shaded_5_bits[] = {0x0003,0x3000,0x0030,0x0000,0xc000,0x0000,
  177.                   0x0000,0x0300};
  178. static short  shaded_6_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc000,0x0000,
  179.                   0x0000,0x0300};
  180. static short  shaded_7_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc000,0x0000,
  181.                   0x0003,0x0300};
  182. static short  shaded_8_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc000,0x0c00,
  183.                   0x0003,0x0300};
  184. static short  shaded_9_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc00c,0x0c00,
  185.                   0x0003,0x0300};
  186. static short shaded_10_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc00c,0x0c00,
  187.                   0x0033,0x0300};
  188. static short shaded_11_bits[] = {0x0003,0x3000,0x0030,0x0c00,0xc00c,0x0c00,
  189.                   0x0033,0x3300};
  190. static short shaded_12_bits[] = {0x0003,0x3000,0x0033,0x0c00,0xc00c,0x0c00,
  191.                   0x0033,0x3300};
  192. static short shaded_13_bits[] = {0x0003,0x3300,0x0033,0x0c00,0xc00c,0x0c00,
  193.                   0x0033,0x3300};
  194. static short shaded_14_bits[] = {0x0003,0x3300,0x0033,0x0c00,0x3f33,0x0c00,
  195.                   0x0033,0x3300};
  196. static short shaded_15_bits[] = {0x0033,0x3300,0x0033,0x0c00,0x3f33,0x0c00,
  197.                   0x0033,0x3300};
  198.  
  199. short *shades_d[16] = {
  200.     shaded_0_bits,  shaded_1_bits,  shaded_2_bits,  shaded_3_bits,
  201.     shaded_4_bits,  shaded_5_bits,  shaded_6_bits,  shaded_7_bits,
  202.     shaded_8_bits,  shaded_9_bits,  shaded_10_bits, shaded_11_bits,
  203.     shaded_12_bits, shaded_13_bits, shaded_14_bits, shaded_15_bits };
  204.  
  205. #if 0
  206. unsigned short v1[] = {
  207.   0, 1507, 2131, 2610, 3014, 3369, 3691, 3986, 4261, 4519, 4763, 4996,
  208.   5217, 5430, 5635, 5832, 6023, 6208, 6388, 6562, 6732, 6898, 7060,
  209.   7218, 7373, 7524, 7673, 7819, 7962, 8102, 8240, 8376, 8509, 8640,
  210.   8770, 8897, 9023, 9147, 9269, 9389, 9508, 9626, 9742, 9856, 9970,
  211.   10082, 10193, 10302, 10410, 10518, 10624, 10729, 10833, 10936, 11037,
  212.   11138, 11239, 11338, 11436, 11533, 11630, 11726, 11821, 11915
  213.   };
  214. #endif
  215.  
  216. unsigned short v[] = {
  217.   4763, 6732, 8240, 9508,
  218.   10624, 11630, 12553, 13411, 14215, 14974, 15695, 16381, 17039, 17670,
  219.   18278, 18865, 19432, 19982, 20516, 21034, 21539, 22031, 22511, 22979,
  220.   23437, 23885, 24323, 24753, 25174, 25586, 25991, 26389, 26780, 27164,
  221.   27541, 27912, 28278, 28637, 28992, 29340, 29684, 30023, 30357, 30687,
  222.   31011, 31332, 31649, 31961, 32269, 32574, 32875, 33172, 33465, 33756,
  223.   34043, 34326, 34607, 34884, 35158, 35429, 35698, 35963, 36226, 36486,
  224.   36743, 36998, 37250, 37500, 37747, 37992, 38235, 38475, 38713, 38948,
  225.   39182, 39413, 39643, 39870, 40095, 40318, 40539, 40759, 40976, 41191,
  226.   41405, 41617, 41827, 42035, 42242, 42446, 42650, 42851, 43051, 43249,
  227.   43446, 43641, 43834, 44026, 44216, 44405, 44593, 44779, 44963, 45146,
  228.   45328, 45509, 45688, 45865, 46041, 46216, 46390, 46562, 46733, 46903,
  229.   47072, 47239, 47405, 47570, 47734, 47896, 48058, 48218, 48377, 48535,
  230.   48692, 48847, 49002, 49155, 49307, 49459, 49609, 49758, 49906, 50053,
  231.   50199, 50344, 50488, 50631, 50773, 50914, 51054, 51193, 51331, 51468,
  232.   51605, 51740, 51874, 52008, 52140, 52272, 52402, 52532, 52661, 52789,
  233.   52916, 53042, 53168, 53292, 53416, 53539, 53661, 53782, 53902, 54022,
  234.   54140, 54258, 54375, 54491, 54607, 54721, 54835, 54948, 55061, 55172,
  235.   55283, 55393, 55502, 55611, 55718, 55825, 55932, 56037, 56142, 56246,
  236.   56349, 56452, 56554, 56655, 56755, 56855, 56954, 57052, 57150, 57247,
  237.   57343, 57439, 57534, 57628, 57722, 57814, 57907, 57998, 58089, 58179,
  238.   58269, 58358, 58446, 58534, 58621, 58707, 58793, 58878, 58962, 59046,
  239.   59129, 59212, 59294, 59375, 59456, 59536, 59615, 59694, 59772, 59850,
  240.   59927, 60004, 60079, 60155, 60229, 60303, 60377, 60450, 60522, 60594,
  241.   60665, 60735, 60805, 60875, 60944, 61012, 61080, 61147, 61213, 61279,
  242.   61345, 61410, 61474, 61538, 61601, 61664, 61726, 61787, 61848, 61909,
  243.   61969, 62028, 62087, 62145, 62203, 62260, 62317, 62373, 62429, 62484,
  244.   62539, 62593, 62646, 62699, 62752, 62804, 62855, 62906, 62956, 63006,
  245.   63056, 63104, 63153, 63201, 63248, 63295, 63341, 63387, 63432, 63477,
  246.   63521, 63565, 63608, 63651, 63693, 63735, 63776, 63817, 63857, 63897,
  247.   63936, 63975, 64013, 64051, 64088, 64125, 64161, 64197, 64233, 64267,
  248.   64302, 64336, 64369, 64402, 64434, 64466, 64498, 64529, 64559, 64589,
  249.   64619, 64648, 64677, 64705, 64732, 64759, 64786, 64812, 64838, 64863,
  250.   64888, 64912, 64936, 64960, 64983, 65005, 65027, 65048, 65069, 65090,
  251.   65110, 65130, 65149, 65168, 65186, 65204, 65221, 65238, 65254, 65270,
  252.   65285, 65300, 65315, 65329, 65342, 65355, 65368, 65380, 65392, 65403,
  253.   65414, 65424, 65434, 65444, 65453, 65461, 65469, 65477, 65484, 65491,
  254.   65497, 65502, 65508, 65513, 65517, 65521, 65524, 65527, 65530, 65532,
  255.   65533, 65534, 65535, 65535
  256.   };
  257.  
  258. #ifdef PI
  259. #undef PI
  260. #endif
  261. #define PI 3.14159265358979323846  /* Assume not near black hole or in
  262.                       Tennessee */
  263.  
  264. double jtime(), phase();
  265.  
  266. static void hackbits(struct tws * t, int w,
  267.              char * bits, int cx, int cy, int rx, int ry)
  268. {
  269.   double jd, angphase, cphase, aom, cdist, cangdia, csund, csuang;
  270.   short i, *plane;
  271.   short xleft, xright, wxright, bxright, wxleft, bxleft;
  272.   long fxleft, fxright, *fxdir, *fxoth;
  273.   float cap, ratio;
  274.   short shadeindex, shade, **shades;
  275.   const short ww = (w + 15) >> 4;
  276.   
  277.   jd = jtime( t );
  278.   
  279.   angphase = phase( jd, &cphase, &aom, &cdist, &cangdia, &csund, &csuang);
  280.   cap = cos( angphase );
  281.   /* Hack to figure approximate earthlighting. */
  282.   if ( cphase < 0.1 ) cphase = 0.1;
  283.   if ( cphase > 0.9 ) cphase = 0.9;
  284.   ratio = (1.0 - cphase) / cphase;  /* ratio varies from 9.0 to 0.111 */
  285.   shadeindex = (int)( ratio / 9.0 * 15.9999 );
  286.   
  287.   shade = 0x0000;
  288.   shades = doubleflag? shades_d: shades_s;
  289.  
  290.   plane = ((short *)bits) + (cy - ry) * ww;
  291.  
  292.   if (angphase >= 0.0 && angphase < PI) {
  293.     fxdir = &fxright;
  294.     fxoth = &fxleft;
  295.   }
  296.   else {
  297.     fxdir = &fxleft;
  298.     fxoth = &fxright;
  299.   }
  300.   for (i = 0; i < 2 * ry; i++) {
  301.     if (i > ry)
  302.       fxright = rx * v[753 - i * 377 / ry];
  303.     else
  304.       fxright = rx * v[i * 377 / ry];
  305.  
  306. /*    printf("%ld, %ld, %d \n", i * 377 / ry, 753 - i * 377 / ry, fxright); */
  307.     fxleft = - fxright;
  308.  
  309.     *fxdir = ((int)((double)*fxdir * cap)) >> 16;
  310.     *fxoth >>= 16;
  311.  
  312.     xright = fxright + cx;
  313.     xleft = fxleft + cx;
  314.     
  315.     wxright = xright >> 4;
  316.     bxright = xright & 15;
  317.     
  318.     wxleft = xleft >> 4;
  319.     bxleft = xleft & 15;
  320.     
  321.     plane += ww;
  322.     
  323.     if ( shadeflag )
  324.       shade = shades[shadeindex][i & 7];
  325.     
  326.     if (wxleft == wxright)
  327.       plane[wxleft] &= leftmask[bxleft] | shade | rightmask[bxright];
  328.     else {
  329.       short x;
  330.       plane[wxleft] &= leftmask[bxleft] | shade;
  331.       for (x = wxleft + 1; x < wxright; x++)    plane[x] &= shade;
  332.       plane[wxright] &= rightmask[bxright] | shade;
  333.     }
  334.   }
  335. }
  336.